<template>
  <el-tooltip :content="t('common.base.dial')" placement="top">
  <i
      v-if="isShowBizCallIcon" 
      @click.stop="handlerClickIconPhoneCall" 
      class="iconfont icon-dianhua1 biz-call-icon"
    >
    </i>
  </el-tooltip>
</template>

<script>
/* service */
import CallCenterService from '@service/CallCenterService';
/* util */
import { getRootWindow } from '@src/util/dom';
import { ctiCallOut } from '@src/util/ctisdk';
import Log from '@src/util/log.ts';
import CallCenterZhiChiUtil from '@src/util/callcenter/CallcenterZhiChi.ts'
import { t } from '@src/locales';

export default {
  name: 'biz-call-icon',
  props: {
    phone: {
      type: String,
      default: ''
    },
    value: {
      type: String,
      default: ''
    },
    sourceData: {
      type: Object,
      default: ()=>{}
    }
  },
  data() {
    return {
      rootWindow: getRootWindow(window)
    }
  },
  computed: {
    /** 
     * @description 是否绑定了呼叫中心坐席
    */
    hasCallCenterAgent() {
      return localStorage.getItem('call_center_agent') == 1
    },
    /** 
     * @description 是否有中通天鸿呼叫中心权限
    */
    hasZhongTongCallAuth() {
      return CallCenterService.getIsCallCenterZhongTong()
    },
    /** 
     * @description 是否智齿智齿呼叫中心
    */
    hasZhiChiAuth() {
      return CallCenterService.getIsCallCenterZhiChi()
    },
    /** 
     * @description 是否有天润呼叫中心
    */
    hasTianRunAuth() {
      return CallCenterService.getIsCallCenterTianRun()
    },
    isHavePhone() {
      return Boolean(this.phoneNumber)
    },
    /**
     * @description 是否显示呼叫按钮
     * 1. 有呼叫中心模块 且有中通天鸿呼叫中心权限
     * 2. 或者 有智齿呼叫中心
     */
    isShowBizCallIcon() {
      
      // 无电话号码，不显示呼叫按钮
      if (!this.isHavePhone) {
        return false
      }

      if(!this.hasCallCenterAgent){
        return false
      }
      
      return this.hasZhongTongCallAuth || this.hasZhiChiAuth || this.hasTianRunAuth
    },
    phoneNumber() {
      return this.value || this.phone
    }
  },
  methods: {
    t,
    handlerClickIconPhoneCall() {

      if(this?.sourceData?.sourceType) sessionStorage.setItem('callCenterSourceData',JSON.stringify(this.sourceData))

      if (this.hasZhiChiAuth)  return this.handlerClickIconPhoneCallZhiChi()
      
      if (this.hasZhongTongCallAuth) return this.handlerClickIconPhoneCallCTI()

      if (this.hasTianRunAuth) return this.handlerClickIconPhoneTianRun()
      
    },
    // 智齿外呼电话
    handlerClickIconPhoneCallZhiChi() {
      try {
        CallCenterZhiChiUtil.agentCallOut(this.phoneNumber)
      } catch (error) {
        Log.error(error, this.handlerClickIconPhoneCallZhiChi.name);
      }
    },
    // 中通外呼电话
    handlerClickIconPhoneCallCTI() {
      try {
        ctiCallOut(this.phoneNumber)
      } catch (error) {
        Log.error(error, this.handlerClickIconPhoneCallCTI.name);
      }
    },
    // 天润外呼电话
     handlerClickIconPhoneTianRun() {
      try {
        this.rootWindow.previewOutcall(this.phoneNumber);
      } catch (error) {
        Log.error(error, this.handlerClickIconPhoneTianRun.name);
      }
    },
  }
}
</script>

<style lang="scss">
.biz-call-icon {
  cursor: pointer;
  color: $color-primary;
  padding-left: 5px;
  font-size: 16px;
}
</style>